package com.xcity.db.type;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.ibatis.type.BaseTypeHandler;
import org.apache.ibatis.type.JdbcType;

import com.alibaba.fastjson.JSON;
import com.xcity.game.map.MineReport;
import com.xcity.game.map.MineReportManager;

import naga.x.util.StringUtils;

public class MineReportsTypeHandler extends BaseTypeHandler<MineReportManager>{

	@Override
	public void setNonNullParameter(PreparedStatement ps, int i, MineReportManager parameter, JdbcType jdbcType)
			throws SQLException {
		ps.setString(i, parameter != null? parameter.toString() : "[]");
	}

	@Override
	public MineReportManager getNullableResult(ResultSet rs, String columnName) throws SQLException {
		return parse(rs.getString(columnName));
	}

	@Override
	public MineReportManager getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
		return parse(rs.getString(columnIndex));
	}

	@Override
	public MineReportManager getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
		return parse(cs.getString(columnIndex));
	}
	
	public static MineReportManager parse(String json){
		if(StringUtils.isEmpty(json)){
			return new MineReportManager(null, null);
		}
		return new MineReportManager(null, JSON.parseArray(json, MineReport.class));
	}
}
